<?php
/**
 * 示例服务类
 * @version 1.0
 * @author: HuangYu <992287102@qq.com>
 * @since: 2019/4/2 15:27
 * Class DemoService
 * @package app\index\service
 */

namespace app\index\service;

use app\index\model\DemoModel;
use app\index\model\DemoOneModel;
use think\Db;

class DemoService
{
    private $demoModel;

    /**
     * 初始化
     */
    public function __construct()
    {
        //初始化模型
        $this->demoModel = new DemoModel();
    }

    /**
     * 示例列表
     * @author HuangYu
     * @since 2019/4/2
     * @param array $param 参数
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function demoList($param = [])
    {
        //组装查询条件
        $where = [];
        if (isset($param['start_time']) && $param['start_time'] !== '') {
            $where[] = ['create_time', '>=', $param['start_time']];
        }
        if (isset($param['end_time']) && $param['end_time'] !== '') {
            $where[] = ['create_time', '<=', $param['end_time']];
        }
        if (isset($param['keyword']) && $param['keyword'] !== '') {
            $where[] = ['title', 'like', '%' . $param['keyword'] . '%'];
        }
        $where[] = ['is_delete', '=', 0];
        //排序
        $sort = [];
        if (isset($param['sort']) && $param['sort']) {
            $sort = $param['sort'];
        }
        if (!isset($sort['id'])) {
            $sort['id'] = 'desc';
        }

        //查询字段
        $field = ['id', 'title', 'remark', 'create_time', 'update_time'];

        //数据操作

        //方式1
        $query = $this->demoModel->field($field)
            ->with(['demo_one', 'demo_many'])
            ->where($where);
        //统计条数
        $count = $query->cache(60)->count();
        //获取列表
        $list = $query->page($param['page'], $param['page_size'])
            ->order($sort)
            ->cache(60)
            ->select();

        //方式2
        /*$query = Db::name('demo')->field($field)
            ->where($where);
        $count = $query->cache(60)->count();
        $list  = $query->page($param['page'], $param['pageSize'])
            ->order($sort)
            ->cache(60)
            ->select();*/

        return ['count' => $count, 'list' => $list];
    }

    /**
     * 示例详情
     * @author HuangYu
     * @since 2019/4/2
     * @param array $param 参数
     * @return array|null|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function demoDetail($param = [])
    {
        //查询条件
        $where = [
            'id' => $param['demo_id']
        ];
        //查询字段
        $field = ['id', 'title', 'remark', 'create_time', 'update_time'];

        //数据操作

        //方式2
        $this->demoModel = new DemoModel();
        $row1 = $this->demoModel->field($field)
            ->where($where)
            ->find();
        //方式2
        $row2 = Db::name('demo')->field($field)
            ->where($where)
            ->find();

        return [$row1,$row2];
    }

    /**
     * 示例添加
     * @author HuangYu
     * @since 2019/4/2
     * @param array $param 参数
     * @return bool|mixed
     */
    public function demoAdd($param = [])
    {
        //数据操作
        $model  = new  DemoModel();
        $result = $model->save($param);
        if ($result === false) {
            return false;
        } else {
            return $model['id'];
        }
    }

    /**
     * 示例编辑
     * @author HuangYu
     * @since 2019/4/2
     * @param array $param 参数
     * @return bool
     */
    public function demoUpdate($param = [])
    {
        $demoId = (int)$param['demo_id'];

        //数据操作
        //方式1
        $model = $this->demoModel->get($demoId);

        //方式2
        //$model  = $this->demoModel->where(['id'=>$demoId])->find();
        if (!$model) {
            return false;
        }
        $result = $model->save($param);
        if ($result === false) {
            return false;
        } else {
            return $model['id'];
        }
    }

    /**
     * 示例一对一关系添加
     * @author HuangYu
     * @since 2019/4/2
     * @param array $param 参数
     * @return bool|mixed
     */
    public function demoOneAdd($param = [])
    {
        //数据操作
        $model  = new  DemoOneModel();
        $result = $model->save($param);
        if ($result === false) {
            return false;
        } else {
            return $model['id'];
        }
    }

    /**
     * 事务操作示例
     * @author HuangYu
     * @since 2019/4/2
     * @param array $param 参数
     * @param array $paramOne 参数
     * @return bool
     */
    public function groupAdd($param = [], $paramOne = [])
    {
        // 启动事务
        Db::startTrans();
        try {
            $demoId = $this->demoAdd($param);
            if (!$demoId) {
                // 回滚事务
                Db::rollback();

                return false;
            }
            $paramOne['demo_id'] = $demoId;
            $demoOneId           = $this->demoOneAdd($paramOne);
            if (!$demoOneId) {
                // 回滚事务
                Db::rollback();

                return false;
            }

            // 提交事务
            Db::commit();

            return true;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();

            return false;
        }
    }
}